package com.baili.dao.distribution;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.baili.dao.base.BaseDao;
import com.baili.dto.ProductOrder;

/**
 * @author iceX
 * @version 创建时间：2016年4月14日 下午5:42:10
 * @Name: DistributionDaoImpl.java
 */
@Repository("DistributionDao")
public class DistributionDaoImpl extends BaseDao implements DistributionDao {

	@Override
	public List<Map<String, Object>> getDistributionList(
			ProductOrder productOrder) {

		List<Map<String, Object>> disLst = this.sqlSessionTemplate.selectList(
				"distribution.getDistributionListProInfo", productOrder);

		List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		List<Map<String, Object>> resultLst = new ArrayList<Map<String, Object>>();

		if (disLst != null && disLst.size() > 0) {

			for (Map<String, Object> disMap : disLst) {

				if (disMap != null) {

					productOrder.setScheduleNo(disMap.get("scheduleNo")
							.toString());

					productOrder.setProductCode(disMap.get("productCode")
							.toString());

					lst = this.sqlSessionTemplate.selectList(
							"distribution.getDistributionList", productOrder);

					if (lst != null && lst.size() > 0) {

						resultLst = this.sqlSessionTemplate.selectList(
								"distribution.getDistributionListResult",
								productOrder);

						if (resultLst != null && resultLst.size() > 0) {

							Map<String, Object> resultMap = resultLst.get(0);

							Date nowDate = new Date();

							int ifOutdate = 0;

							Date d_overTime = (Date) resultMap.get("overTime");

							long day = (nowDate.getTime() - d_overTime
									.getTime()) / (24 * 60 * 60 * 1000);

							if (day <= 0) {

								ifOutdate = 1;

								resultMap.put("ifOutdate", ifOutdate);

							} else if (day > 0) {

								ifOutdate = 2;

								resultMap.put("state", ifOutdate);
							}

							list.add(resultMap);
						}
					}
				}
			}
		}

		return list;
	}

	@Override
	public List<Map<String, Object>> getDistributionListBPOI(
			ProductOrder productOrder) {
		
		List<Map<String, Object>> disLst = new ArrayList<Map<String,Object>>();
		
		List<Map<String, Object>> totalLst = new ArrayList<Map<String,Object>>();
		totalLst = this.sqlSessionTemplate.selectList(
				"distribution.getDistributionListBPOITotal", productOrder);
		
		if(totalLst != null && totalLst.size()>0){
			
			int productCount = Integer.parseInt(totalLst.get(0).get("productCount").toString());
			
			if(productOrder.getMatchingStaffType() >0){
				
				disLst = this.sqlSessionTemplate.selectList(
						"distribution.getDistributionListBPOIEd", productOrder);
			}else {
				
				disLst = this.sqlSessionTemplate.selectList(
						"distribution.getDistributionListBPOI", productOrder);
			}
			
			List<Map<String, Object>> lst = new ArrayList<Map<String,Object>>();
			
			if (disLst != null && disLst.size() > 0) {
				
				if(productOrder.getMatchingStaffType() >0){
					
					disLst = this.sqlSessionTemplate.selectList(
							"distribution.getDistributionListBPOITwoEd", productOrder);
				}else {
					
					disLst = this.sqlSessionTemplate.selectList(
							"distribution.getDistributionListBPOITwo", productOrder);
				}
				
				for (Map<String, Object> disMap : disLst) {
					
					if (disMap != null) {
						
						productOrder.setScheduleNo(disMap.get("scheduleNo").toString());
						
						productOrder.setFinishedProductName(disMap.get("productName").toString());
						
						List<Map<String, Object>> bpoipLst = this.getDistributionListBPOIP(productOrder);
						
						if(bpoipLst != null && bpoipLst.size() > 0){
							
							for (Map<String, Object> rMap : bpoipLst) {
								
								if (rMap != null) {
									
									Date nowDate = new Date();
									
									int ifOutdate = 0;
									
									Date d_overTime = (Date) rMap.get("overTime");
									
									long day = (nowDate.getTime() - d_overTime.getTime())
											/ (24 * 60 * 60 * 1000);
									
									if (day <= 0) {
										
										ifOutdate = 1;
										
										disMap.put("ifOutdate", ifOutdate);
										
									} else if (day > 0) {
										
										ifOutdate = 2;
										
										disMap.put("ifOutdate", ifOutdate);
										
										break;
									}
								}
							}
							
							disMap.put("productCount", productCount);
							
							lst.add(disMap);
						}
					}
				}
			}
			return lst;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getDistributionListBPOIP(
			ProductOrder productOrder) {

		List<Map<String, Object>> lst = new ArrayList<Map<String,Object>>();
		
		productOrder.setProcessName("帮面");
		List<Map<String, Object>> pbList = this.sqlSessionTemplate.selectList("distribution.getDistributionListBPOIPKZ", productOrder);

		productOrder.setProcessName("中底");
		List<Map<String, Object>> zdList = this.sqlSessionTemplate.selectList("distribution.getDistributionListBPOIP", productOrder);

		productOrder.setProcessName("成型底");
		List<Map<String, Object>> cxdList = this.sqlSessionTemplate.selectList("distribution.getDistributionListBPOIP", productOrder);
		
		//可配帮
		int distributCount = 0;
		//已配帮
		int distributedCount = 0;
		
		for (Map<String, Object> pbMap : pbList) {

			if (pbMap != null) {

				String pbSize = pbMap.get("size").toString();

				for (Map<String, Object> zdMap : zdList) {

					if (zdMap != null) {

						String zdSize = zdMap.get("size").toString();
						
						if (pbSize.equals(zdSize)) {
							
							for (Map<String, Object> cxdMap : cxdList) {

								if (cxdMap != null) {

									String cxdSize = cxdMap.get("size").toString();
									
									if (zdSize.equals(cxdSize)) {
										
										int minProductCount = Math.min(Math.min(
												Integer.parseInt(zdMap.get("productCount").toString())
												,Integer.parseInt(pbMap.get("productCount").toString()))
												,Integer.parseInt(cxdMap.get("productCount").toString()));
											
										distributCount = distributCount + (minProductCount - Integer.parseInt(pbMap.get("matchingStaffCount").toString()));
											
										pbMap.put("productCount", minProductCount);
										
										if((productOrder.getMatchingStaffType()== 0)){
											
											if(!pbMap.get("productCount").toString().equals(pbMap.get("matchingStaffCount").toString())){
												
												lst.add(pbMap);
											}
										}else if((productOrder.getMatchingStaffType()== 1)){
											
											if(Integer.parseInt(pbMap.get("matchingStaffCount").toString()) > 0){
												
												lst.add(pbMap);
											}
										}
										distributedCount = distributedCount+Integer.parseInt(zdMap.get("matchingStaffCount").toString());
									}
								}
							}
						}
					}
				}
			}
		}
		
		int id = 1;
		
		for (Map<String, Object> map : lst) {
			
			map.put("id", id++);
			
			map.put("distributedCount", distributedCount);
			
			//map.put("distributCount", distributCount-distributedCount);
			map.put("distributCount", distributCount);
		}

		return lst;
	}

	@Override
	public List<Map<String, Object>> getDistributionDetail(
			ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.getDistributionDetail", productOrder);
	}

	@Override
	public int getDistributionCount() {

		return this.sqlSessionTemplate
				.selectOne("distribution.getDistributionCount");
	}

	@Override
	public int noMatchingStaffTypeCount() {

		return this.sqlSessionTemplate
				.selectOne("distribution.noMatchingStaffTypeCount");
	}

	@Override
	public int matchingStaffTypeCount() {

		return this.sqlSessionTemplate
				.selectOne("distribution.matchingStaffTypeCount");
	}

	@Override
	public List<Map<String, Object>> getDistributionByNormed(
			ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.getDistributionByNormed", productOrder);
	}

	@Override
	public List<Map<String, Object>> getNormedCountB(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.getNormedCountB", productOrder);
	}

	@Override
	public List<Map<String, Object>> getNormedCountZ(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.getNormedCountZ", productOrder);
	}

	@Override
	public List<Map<String, Object>> getNormedCountC(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.getNormedCountC", productOrder);
	}

	@Override
	public void updateDistribution(ProductOrder productOrder) {
		//获取该排成号下的该鞋号下的额定数量以及配帮数量
		List<Map<String,Object>> DFClst = this.sqlSessionTemplate.selectList("distribution.getDistributionFinishedCount",productOrder);
		
		for(Map<String, Object> DFCMap : DFClst){
			
			int productCount = Integer.parseInt(DFCMap.get("productCount").toString());
			
			int matchingStaffCount = Integer.parseInt(DFCMap.get("matchingStaffCount").toString());
			
			int inputCount = productOrder.getMatchingStaffCount();
			
			if(inputCount+matchingStaffCount > productCount){
				
				return;
				
			}else if(inputCount+matchingStaffCount == productCount){
				
				productOrder.setMatchingStaffCount(inputCount+matchingStaffCount);
				
			//	productOrder.setMatchingStaffType(1);
				
				productOrder.setShowMark(1);
				
				this.sqlSessionTemplate.update("distribution.updateDistribution",productOrder);
				
			}else{
				
				productOrder.setMatchingStaffCount(inputCount+matchingStaffCount);
				
//				productOrder.setMatchingStaffType(0);
				
				productOrder.setShowMark(1);
				
				this.sqlSessionTemplate.update("distribution.updateDistribution",productOrder);
			}
		}
	}

	@Override
	public void updateDistributionByNormed() {

		this.sqlSessionTemplate
				.selectOne("distribution.updateDistributionByNormed");
	}

	@Override
	public List getDistributionBySize(ProductOrder productOrder) {

		return this.sqlSessionTemplate.selectList(
				"distribution.updateDistributionByNormed", productOrder);
	}

}
